from db import utils
from logger import logger
from sub_events.base_event import BaseEvent

LOGGER = logger.CustomLogger()

class RestoreReview(BaseEvent):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.event_name_zh: str = "恢复加分"
        self.event_name_en: str = "Restore Review"

    def execute(self) :
        owners = utils.get_code_owners(hook=self.hook)
        comments = ""
        for owner in owners:
            if owner.owner_name not in self.cfg.get("accounts", {}).get("groups"):
                continue
            if owner.passed:
                group = owner.owner_name
                access_token = self.cfg.get("accounts", {}).get("groups")[group]
                response = self.git_client.review(self.hook.pr_number, token=access_token)
                LOGGER.info(
                    f"""[{self.hook.pr_number}] restore {group} review, response code {response.status_code}""")
                comments += f"@{group} 加分已恢复。\n\n"
        self.comment = comments
